import { stringEx } from "./stringEx";

 
//时间日期处理类
class okDateTime
{
    
// 日期，在原有日期基础上，增加days天数，默认增加1天
  static addDate(date, days) {
    if (days == undefined || days == '') {
      days = 1;
    } 
    date.setDate(date.getDate() + days); 
    return date
  } 
  
  
  //将日期类格式化成字符串
  static formatTime (date){
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  }
  
  
  //去掉日期中的秒部分
  static removeDateSecond(dateStr)
  {
    return dateStr.length>5?dateStr.substr(0,dateStr.length-3):dateStr;
  }
  
  //将日期字符串转化为日期类
  //yyyy-MM-dd hh:mm:ss
  //yyyy-MM-dd hh:mm
  //yyyy/MM/dd hh:mm:ss
  //yyyy/MM/dd hh:mm
  static convertDateFromString(dateString) { 
      dateString = dateString.replace(/-/g, "/")
      var arr1 = dateString.split(" ");
      var sdate = arr1[0].split('/');

      if(!arr1[1])
        return new Date(sdate[0], sdate[1] - 1, sdate[2])      

      var stime = arr1[1].split(':');
      var date = new Date(
        sdate[0], sdate[1] - 1, sdate[2],
        stime[0], stime[1], stime.length>=3?stime[2]:0
      );
      return date;
  }

 

  //将0补齐为00格式
  static formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  //从一个日期到另一个时期经过了多长时间的字符串形式
  //{from,to}
  static fmtLostTime(obj)
  {
    //经过的秒数
    var lostSecend = Math.floor( (obj.to.getTime() - obj.from.getTime())/1000)
    var h=0,m=0,s=0
    s = lostSecend%60
    m = Math.floor(lostSecend/60)%60
    h = Math.floor(lostSecend/3600)

   // console.log("fmtT",lostSecend,s,m,h)
    var re = this.formatNumber(s)+"秒"

    if(m>0||h>0) 
        re=this.formatNumber(m)+"分"+re

    if(h>0) re=h+"时"+re

    return re
  }
}

export {okDateTime}